/*
 * ========================================================
 * Copyright(c) 2014 杭州偶尔科技-版权所有
 * ========================================================
 * 本软件由杭州偶尔科技所有, 未经书面许可, 任何单位和个人不得以
 * 任何形式复制代码的部分或全部, 并以任何形式传播。
 * 公司网址
 * 
 * 			http://www.kkkd.com/
 * 
 * ========================================================
 */
package com.simon.mengkou.ui.activity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;
import com.ouertech.android.agm.lib.base.future.core.AgnettyFuture;
import com.ouertech.android.agm.lib.base.future.core.AgnettyResult;
import com.ouertech.android.agm.lib.base.utils.UtilList;
import com.ouertech.android.agm.lib.ui.base.defaults.activity.BaseTopActivity;
import com.simon.mengkou.R;
import com.simon.mengkou.data.bean.base.Brand;
import com.simon.mengkou.future.base.OuerFutureListener;
import com.simon.mengkou.system.constant.CstOuer;
import com.simon.mengkou.system.global.OuerApplication;
import com.simon.mengkou.ui.adapter.BrandAdapter;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * @author : Zhenshui.Xia
 * @since : 2016/3/28.
 * desc    : 品牌详情
 */
public class BrandActivity extends BaseTopActivity{
    //品牌列表
    @Bind(R.id.common_pulltorefresh_id_grid)
    PullToRefreshGridView mPtrView;

    private BrandAdapter mAdapter;

    //品牌id
    private String mId;
    //标题
    private String mTitle;
    //品牌
    private Brand mBrand;

    @Override
    protected void init(Bundle savedInstanceState) {
        super.init(savedInstanceState);
        if (Intent.ACTION_VIEW.equals(getIntent().getAction())) {
            Uri uri = getIntent().getData();
            mId = uri.getQueryParameter(CstOuer.KEY.ID);
        } else {
            Intent intent = getIntent();
            mId = intent.getStringExtra(CstOuer.KEY.ID);
            mTitle = intent.getStringExtra(CstOuer.KEY.TITLE);
        }
    }

    @Override
    protected void initTop() {
        setTitle(mTitle);
        setNavigation(R.drawable.common_ic_left_nav);
    }


    @Override
    protected void initLayout() {
        setContentView(R.layout.layout_common_pulltorefresh_grid);
    }

    @Override
    protected void initViews() {
        ButterKnife.bind(this);

        mPtrView.setMode(PullToRefreshBase.Mode.DISABLED);
        mAdapter = new BrandAdapter(this);
        mPtrView.setAdapter(mAdapter);

        //首次失败重试
        setOnRetryListener(new OnRetryListener() {
            @Override
            public void onRetry() {
                getData();
            }
        });

        //获取数据
        getData();
    }


    /**
     * 获取品牌详情
     */
    private void getData() {
        AgnettyFuture future = OuerApplication.mOuerFuture.brandDetail(mId,
                new OuerFutureListener(this) {
                    @Override
                    public void onStart(AgnettyResult result) {
                        super.onStart(result);
                        setLoading(true);
                    }

                    @Override
                    public void onComplete(AgnettyResult result) {
                        super.onComplete(result);
                        setLoading(false);
                        mBrand = (Brand) result.getAttach();

                        //设置标题
                        setTitle(mBrand.getTitle());
                        //设置品牌数据
                        if(UtilList.isEmpty(mBrand.getGoods())) {
                            setEmptyText(R.string.common_empty_data);
                        } else {
                            mAdapter.setList(mBrand.getGoods());
                        }
                    }

                    @Override
                    public void onException(AgnettyResult result) {
                        super.onException(result);
                        setRetry(true);
                    }

                    @Override
                    public void onNetUnavaiable(AgnettyResult result) {
                        super.onNetUnavaiable(result);
                        setRetry(true);
                    }
                });

        attachDestroyFutures(future);
    }
}
